package com.boot.main.service.impl;

import com.boot.main.entity.CcomUser;
import com.boot.main.entity.ScomAd;
import com.boot.main.mapper.CcomUserMapper;
import com.boot.main.mapper.ScomAdMapper;
import com.boot.main.response.ResponseData;
import com.boot.main.response.ResponseDataUtil;
import com.boot.main.service.IAdService;
import com.boot.main.utils.IsNotEmptyUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdService implements IAdService {
    @Resource
    private ScomAdMapper adMapper;

    @Resource
    private CcomUserMapper userMapper;

    public Long getUserId(){
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals =  subject.getPrincipals();
        String username = (String) principals.getPrimaryPrincipal();
        CcomUser user = userMapper.getUserByUserName(username);
        Long id = user.getId();
        return id;
    }

    @Override
    public ResponseData getAds(Integer page, Integer limit, String name){
        Long userId = getUserId();
        if (limit == 5) {
            CcomUser user = userMapper.getUserByUserName("superAdmin");
            userId = user.getId();
        }
        List<ScomAd> items = adMapper.getAds((page-1)*limit, limit, name, userId);
        Integer count = adMapper.getTotalCount(userId);
        Map map = new HashMap();
        map.put("datas",items);
        map.put("count", count);
        return ResponseDataUtil.success("获取Banner成功", map);
    }

    @Override
    public ResponseData getAdminAds(Integer page, Integer limit, String name){
        CcomUser user = userMapper.getUserByUserName("superAdmin");
        List<ScomAd> items = adMapper.getAds((page-1)*limit, limit, name, user.getId());
        Integer count = adMapper.getTotalCount(user.getId());
        Map map = new HashMap();
        map.put("datas",items);
        map.put("count", count);
        return ResponseDataUtil.success("获取Banner成功", map);
    }

    @Override
    public ResponseData saveAd(ScomAd ad){
        ad.setCreate_time(new Date().getTime());
        Integer num = adMapper.saveAd(ad);
        if(IsNotEmptyUtil.isEmpty(num)){
            return ResponseDataUtil.error("保存Banner失败", null);
        }
        return ResponseDataUtil.success("保存Banner成功", null);
    }

    @Override
    public ResponseData updateAd(ScomAd ad){
        Integer num = adMapper.updateAd(ad);
        if(IsNotEmptyUtil.isEmpty(num)){
            return ResponseDataUtil.error("修改Banner失败", null);
        }
        return ResponseDataUtil.success("修改Banner成功", null);
    }

    @Override
    public ResponseData deleteAd(Long id){
        Integer num = adMapper.deleteAd(id);
        return ResponseDataUtil.success("删除Banner成功", null);
    }
}
